package tr.richfacesext.components.imgviewer;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

/**
 * 
 * @author mert
 *
 */
public class ImgViewer extends UIOutput{

	private Boolean _thumbnail;
	private Integer _width;
	private Integer _height;
	private Integer _thumbWidth;
	private Integer _thumbHeight;
	private String _style;
	private String _onclick;

	public ImgViewer() {
		setRendererType(ImgViewerConstants.DEFAULT_RENDERER);
	}

	public String getFamily() {
		return ImgViewerConstants.COMPONENT_FAMILY;
	}

	public Boolean getThumbnail() {
		if(_thumbnail != null )
			return _thumbnail;

		ValueBinding vb = getValueBinding("thumbnail");
		return vb != null ? (java.lang.Boolean) vb.getValue(getFacesContext()) : Boolean.FALSE;
	}
	public void setThumbnail(Boolean thumbnailValue) {
		_thumbnail = thumbnailValue;
	}

	public Integer getWidth() {
		if(_width != null )
			return _width;

		ValueBinding vb = getValueBinding("width");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : null;
	}
	public void setWidth(Integer widthValue) {
		_width = widthValue;
	}

	public Integer getHeight() {
		if(_height != null )
			return _height;

		ValueBinding vb = getValueBinding("height");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : null;
	}
	public void setHeight(Integer heightValue) {
		_height = heightValue;
	}

	public Integer getThumbWidth() {
		if(_thumbWidth != null )
			return _thumbWidth;

		ValueBinding vb = getValueBinding("thumbWidth");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 60;
	}
	public void setThumbWidth(Integer thumbWidthValue) {
		_thumbWidth = thumbWidthValue;
	}

	public Integer getThumbHeight() {
		if(_thumbHeight != null )
			return _thumbHeight;

		ValueBinding vb = getValueBinding("thumbHeight");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 60;
	}
	public void setThumbHeight(Integer thumbHeightValue) {
		_thumbHeight = thumbHeightValue;
	}

	public String getStyle() {
		if(_style != null )
			return _style;

		ValueBinding vb = getValueBinding("style");
		return vb != null ? (java.lang.String) vb.getValue(getFacesContext()) : null;
	}
	public void setStyle(String styleValue) {
		_style = styleValue;
	}

	public String getOnclick() {
		if(_onclick != null )
			return _onclick;

		ValueBinding vb = getValueBinding("onclick");
		return vb != null ? (java.lang.String) vb.getValue(getFacesContext()) : null;
	}
	public void setOnclick(String onclickValue) {
		_onclick = onclickValue;
	}

	public Object saveState(FacesContext context) {
		Object values[] = new Object[8];
		values[0] = super.saveState(context);
		values[1] = _thumbnail;
		values[2] = _width;
		values[3] = _height;
		values[4] = _thumbWidth;
		values[5] = _thumbHeight;
		values[6] = _style;
		values[7] = _onclick;
		return ((Object) values);
	}
	public void restoreState(FacesContext context, Object state) {
		Object values[] = (Object[]) state;
		super.restoreState(context, values[0]);
		_thumbnail = (Boolean) values[1];
		_width = (Integer) values[2];
		_height = (Integer) values[3];
		_thumbWidth = (Integer) values[4];
		_thumbHeight = (Integer) values[5];
		_style = (String) values[6];
		_onclick = (String) values[7];
	}
	
	public byte[] getImage() {
		if (getValue() == null)
			return null;
		
		if (getValue() instanceof byte[])
			return (byte[]) getValue();
		
		throw new IllegalArgumentException("value property of component should be a byte array.");
	}
}